<html lang="en">
<head>
    <title>目的地推荐管理</title>
<#include "../common/header.ftl">

    <link type="text/css" rel="stylesheet" href="/js/plugins/uploadifive/uploadifive.css"/>
    <script type="text/javascript" src="/js/plugins/uploadifive/jquery.uploadifive.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/plugins/bootstrap-select/bootstrap-select.css"/>
    <script src="/js/plugins/bootstrap-select/bootstrap-select.js"></script>

    <script type="text/javascript">
        $(function () {


            //图片上传
            $('.imgBtn').uploadifive({
                'auto': true,
                'uploadScript': '/uploadImg',
                buttonClass: "btn-link",
                'fileObjName': 'pic',
                'buttonText': '浏览...',
                'fileType': 'image',
                'multi': false,
                'fileSizeLimit': 5242880,
                'removeCompleted': true, //取消上传完成提示
                'uploadLimit': 10,
                'queueSizeLimit': 10,
                'overrideEvents': ['onDialogClose', 'onError'],    //onDialogClose 取消自带的错误提示
                'onUploadComplete': function (file, data) {
                    $("#imgUrl").attr("src", "/" + data);
                    $("#coverUrl").val("/" + data);

                },
                onFallback: function () {
                    $.messager.alert("温馨提示", "该浏览器无法使用!");
                }
            });


            //编辑/添加
            $(".inputBtn").click(function () {

                $("#imgUrl").attr("src", "/images/default.jpg");

                //数据复原
                $("#editForm").clearForm(true);
                $('#destId').selectpicker('val', '');
                //目的地推荐回显数据
                var json = $(this).data("json");
                console.log(json);
                if (json) {
                    $("#editForm input[name='id']").val(json.id);
                    $("#editForm input[name='coverUrl']").val(json.coverUrl);
                    $("#editForm textarea[name='info']").val(json.info);
                    $("#imgUrl").attr("src", json.coverUrl);
                    var themeType = json.themeType;
                    $(".theme").val(themeType);
                    //显示这个type对应的下拉框
                    $.get("/destinationCommendTheme/getThemeByType",{type:themeType},function (data) {
                        var themes = '';
                        $.each(data, function (index, item) {
                            themes += '<option value="' + item.id + '">'+item.name+'</option>';
                        })
                        $(".themeId").html(themes);
                        $(".themeId").val(json.themeId);
                    });
                    $("#month").val(json.month);
                    $("#days").val(json.days);
                    $("#state").val(json.state);
                    $("#sequence").val(json.sequence);
                    //目的地会先
                    var destId = json.destId;
                    $('#destId').selectpicker('val', destId);
                    $('#destId').selectpicker('refresh');
                }
                //弹出模态框
                $("#editModal").modal("show");

            })

            $(".submitBtn").click(function () {
                //模态框表单提交
                $("#editForm").ajaxSubmit(function (data) {
                    if (data.success) {
                        window.location.reload();
                    } else {
                        $.messager.alert("温馨提示", data.msg)
                    }
                })
            })

            //约定查询目的地: 国家省份城市
            $.get("/region/getDestByDeep", {deep:3}, function (data) {
                var html = '';
                $.each(data, function (index, item) {
                    html += '<option value="' + item.id + '">'+item.name+'</option>'
                })

                $("#destId").html(html);
                $('#destId').selectpicker('refresh'); //刷新组件
            })

            //主题二级联动
            $(".theme").change(function () {
                var type = $(this).val();
                $.get("/destinationCommendTheme/getThemeByType",{type:type},function (data) {
                    var themes = '';
                    $.each(data, function (index, item) {
                        themes += '<option value="' + item.id + '">'+item.name+'</option>';
                    })
                    $(".themeId").html(themes);
                });
            })

            //推荐/取消推荐
            $(".hotBtn").click(function () {
                var state = $(this).data('state');
                var id = $(this).data("id");
                $.get("/destinationCommend/changeStateValue", {state:state, id:id}, function (data) {
                    if(data.success){
                        window.location.reload();
                    }else{
                        $.messager.alert("温馨提示", data.msg);
                    }
                })
            })
        })
    </script>
</head>
<body>
<!--左侧菜单回显变量设置-->
<#assign currentMenu="destinationCommend">

<div class="container-fluid " style="margin-top: 20px">
<#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-2">
        <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">目的地推荐管理</h1>
                </div>
            </div>
        <#setting number_format="#">
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/destinationCommend/list" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <a href="JavaScript:;" class="btn btn-success inputBtn"><span class="glyphicon glyphicon-plus"></span>
                    添加</a>
            </form>

            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>封面</th>
                    <th>地点</th>
                    <th>简介</th>
                    <th>主题</th>
                    <th>推荐旅游月份</th>
                    <th>推荐旅游天数</th>
                    <th>操作</th>
                </tr>
                </thead>
            <#list pageInfo.list as entity>
                <tr>
                    <td>${entity_index+1}</td>
                    <td><img src="${entity.coverUrl!}" width="100px"></td>
                    <td>${entity.dest.name!}</td>
                    <td>${entity.info!}</td>
                    <td>${entity.theme.name!}</td>
                    <td>${entity.month!}</td>
                    <td>${entity.days!}</td>
                    <td>
                        <#if  entity.state == 1>
                            <a class="btn btn-success btn-xs hotBtn" href="javascript:;" data-state='0' data-id="${entity.id}">
                                <span class="glyphicon glyphicon-minus-sign"></span> 取消推荐
                            </a>

                        <#else>
                            <a class="btn btn-default btn-xs hotBtn" href="JavaScript:;" data-state="1" data-id="${entity.id}">
                                <span class="glyphicon glyphicon-tag"></span>设为推荐
                            </a>
                        </#if>

                        <a class="btn btn-info btn-xs inputBtn" href="javascript:;" data-json='${entity.jsonString}'>
                            <span class="glyphicon glyphicon-edit"></span> 编辑
                        </a>

                        <a href="javascript:;" class="btn btn-danger btn-xs deleteBtn"
                           data-url="/destinationCommend/delete?id=${entity.id}">
                            <span class="glyphicon glyphicon-trash"></span> 删除
                        </a>
                    </td>
                </tr>
            </#list>
            </table>
        <#--分页-->
        <#include "../common/page.ftl"/>
        </div>
    </div>
</div>

<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">目的地推荐添加/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/destinationCommend/saveOrUpdate" method="post" id="editForm">
                    <input type="hidden" value="" name="id">

                    <div class="form-group">
                        <label  class="col-sm-3 control-label">推荐目的地：</label>
                        <div class="col-sm-6">
                            <select class="form-control selectpicker " id="destId" name="dest.id"

                                    data-live-search="true" title="请选择关联的目的地">

                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">推荐目的地封面：</label>
                        <div class="col-sm-6">
                            <input type="hidden" class="form-control" id="coverUrl" name="coverUrl" value="">
                            <img src="/images/default.jpg" width="100px" id="imgUrl">
                            <button type="button" class="imgBtn">浏览</button>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">主题类型：</label>
                        <div class="col-sm-6">
                            <select class="form-control theme">
                                <option value="0">全年适宜</option>
                                <option value="1">季节</option>
                                <option value="2">出行方式</option>
                                <option value="3">节假日</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">主题：</label>
                        <div class="col-sm-6">
                            <select class="form-control themeId" id="theme.id" name="theme.id">

                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">推荐旅游月份：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" id="month" name="month" oninput="if(value>12)value=12;if(value<1)value=1" min="1" max="12" required>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">推荐旅游天数：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" id="days" name="days" min="1" oninput="if(value<1)value=1" required>
                        </div>
                    </div>

                    <div class="form-group">
                        <label  class="col-sm-3 control-label">是否推荐：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="state" name="state">
                                <option value="1">推荐</option>
                                <option value="0">普通</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">排序：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sequence" name="sequence" placeholder="请输入序号">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">简介：</label>
                        <div class="col-sm-6">
                            <textarea class="form-control" name="info"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary submitBtn">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>